﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Doctor 的摘要说明
/// </summary>
public class Doctor: LwUser
{
    public const string StrEpxert = "expert";
    public bool Expert;
    public int TeamGuid;
    public int CategoryGuid;
    public string Yjfx;
    public int PaperNum;
    public int MinPaperNum;
    public int MaxPaperNum;
	public Doctor(int g):base(g)
	{
        this.Init();
    }
    public Doctor(string uid)
        : base(uid)
    {
        this.Init();
    }

    public void Init()
    {
        string sql = "select * from Doctor where UserGuid=" + this.Guid.ToString();
        DataRow dr = Fun.CreateDr(sql);
        this.Expert = Convert.ToBoolean(dr["Expert"]);
        this.TeamGuid = Convert.ToInt32(dr["TeamGuid"]);
        this.CategoryGuid=Convert.ToInt32(dr["CategoryGuid"]);
        this.Yjfx = dr["yjfx"].ToString();
        this.MinPaperNum = Convert.ToInt32(dr["MinPaperNum"]);
        this.MaxPaperNum = Convert.ToInt32(dr["MaxPaperNum"]);
        this.PaperNum = Convert.ToInt32(dr["PaperNum"]);
    }

    public DataTable RecversDt()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr;
        dr = dt.NewRow();
        dr["text"] = "管理员";
        dr["value"] = Params.AdministratorGuid;
        dt.Rows.Add(dr);
        string sql = "select u.Guid,u.RealName from Users u Join Student s on u.Guid=s.UserGuid where s.DoctorGuid="+this.Guid+" order by u.UserID asc";
        DataTable dt2 = Fun.CreateDt(sql);
        foreach (DataRow dr2 in dt2.Rows)
        {
            dr = dt.NewRow();
            dr["text"] = dr2["RealName"];
            dr["value"] = dr2["Guid"];
            dt.Rows.Add(dr);
        }
        if (this.Expert)
        {
            sql = "select u.Guid,s.PaperName from Users u Join Student s on u.Guid=s.UserGuid where s.ExpertGuid=" + this.Guid + " order by u.UserID asc";
            dt2 = Fun.CreateDt(sql);
            foreach (DataRow dr2 in dt2.Rows)
            {
                dr = dt.NewRow();
                dr["text"] = dr2["PaperName"];
                dr["value"] = dr2["Guid"];
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }

    public bool DoctorHasStudent(int sg)
    {
        return Doctor.DoctorHasStudent(this.Guid, sg);
    }

    public bool ExpertHasStudent(int sg)
    {
        return Doctor.ExpertHasStudent(this.Guid, sg);
    }

    public string TeamName
    {
        get
        {
            string sql = "select TeamName as n from Team where UserGuid="+this.TeamGuid.ToString();
            return Fun.GetField(sql).ToString().Trim();
        }
    }

    public static bool IsDoctor(int g)
    {
        string sql = "select UserType from Users where Guid=" + g.ToString();
        object obj = Fun.GetField(sql, "UserType");
        if (obj == null)
        {
            return false;
        }
        return obj.ToString().Trim() == LwUser.DoctorType;
    }

    public string CategoryName
    {
        get
        {
            string sql = "select Category as n from Category where Guid=" + this.CategoryGuid.ToString();
            DataTable dt = Fun.CreateDt(sql);
            if (dt.Rows.Count < 1)
            {
                return "无";
            }
            return dt.Rows[0][0].ToString().Trim();
            //return Fun.GetField(sql).ToString().Trim();
        }
    }

    public int StudentCount
    {
        get
        {
            string sql = "select count(*) as n from Student where DoctorGuid="+this.Guid.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int PaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid=" + this.Guid.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int PastPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid=" + this.Guid.ToString()+" and PaperFlag="+Paper.PastFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }

    public int SuspendPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid=" + this.Guid.ToString() + " and PaperFlag=" + Paper.SuspendFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }


    public int FailPaperCount
    {
        get
        {
            string sql = "select count(*) as n from Student where ExpertGuid=" + this.Guid.ToString() + " and PaperFlag=" + Paper.FailFlag.ToString();
            return Convert.ToInt32(Fun.GetField(sql, "n"));
        }
    }
    public override bool Update()
    {
        base.Update();
        string sql = "update Doctor set Yjfx='" + Fun.L(this.Yjfx) + "',Expert=" + (this.Expert ? "1" : "0") + ",TeamGuid=" + this.TeamGuid.ToString() + ",CategoryGuid=" + this.CategoryGuid.ToString() + ",MaxPaperNum=" + this.MaxPaperNum.ToString() + ",MinPaperNum=" + this.MinPaperNum.ToString() + " where UserGuid=" + this.Guid.ToString();
        return Fun.Execute(sql) > 0;
    }

    public static int Count()
    {
        string sql = "select count(*) as n from Users where UserType='"+LwUser.DoctorType+"'";
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static int ExpertCount()
    {
        string sql = "select count(*) as n from Doctor where Expert=1";
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static Doctor Check()
    {
        try
        {
            LwUser lu = (LwUser)System.Web.HttpContext.Current.Session["Loginer"];
            string uid = System.Web.HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Params["uid"]);
            if (lu.UserType == LwUser.AdminType)
            {
                return new Doctor(uid);
            }
            else if (lu.UserType == LwUser.TeamType)
            {
                if(Team.TeamHasDoctor(lu.Guid,uid))
                {
                    return new Doctor(uid);
                }
                else{
                    return null;
                }
            }
            if (lu.UserType == LwUser.DoctorType)
            {
                if (lu.UserID != uid)
                {
                    return null;
                }
                return new Doctor(uid);
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

    public static int Add(string uid, string n, string email, int tg, int cg, bool exp, ref string tip)
    {
        int g = LwUser.Add(uid, n, email, LwUser.DoctorType, ref tip);
        if (g == 0)
        {
            return 0;
        }
        string sql = "select Yjfx from Team where UserGuid="+tg.ToString();
        string yjfx = Fun.GetField(sql,"Yjfx").ToString().Trim();
        sql = "insert into Doctor(Guid,UserGuid,TeamGuid,CategoryGuid,Yjfx,Expert,MinPaperNum,MaxPaperNum) values("+Fun.Guid()+","+
            g.ToString()+","+tg.ToString()+","+cg.ToString()+",'"+Fun.L(yjfx)+"',"+(exp?"1":"0")+","+Params.MinPaperNum.ToString()+","+Params.MaxPaperNum.ToString()+")";
        if (Fun.Execute(sql) > 0)
        {
            return g;
        }
        else
        {
            tip = "添加失败！";
            sql = "delete from Users where Guid="+g.ToString();
            Fun.Execute(sql);
            return 0;
        }
    }

    public static int Add(string uid, string n, string email, string t,  string exp, ref string tip)
    {
        string sql = "select Guid from Users where UserID='"+t.Replace("'","''")+"' and UserType='"+LwUser.TeamType+"'";
        DataTable dt = Fun.CreateDt(sql);
        int tg=0;
        if (dt.Rows.Count < 1)
        {
            tip = "不存在ID为"+t+"的团队";
            return 0;
        }
        else
        {
            tg=Convert.ToInt32(dt.Rows[0][0]);
        }
        bool b=false;
        if (exp == "0")
        {
            b = false;
        }
        else if (exp == "1")
        {
            b = true;
        }
        else
        {
            tip = "expert必须为0或1";
            return 0;
        }
        return Doctor.Add(uid, n, email, tg, 0, b, ref tip);
    }

    public static DataTable Doctors(bool hasAll)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr;
        if (hasAll)
        {
            dr = dt.NewRow();
            dr["text"] = "所有导师";
            dr["value"] = 0;
            dt.Rows.Add(dr);
        }
        string sql = "select Guid,RealName from Users where UserType='"+LwUser.DoctorType+"' order by Guid asc";
        DataTable dt2 = Fun.CreateDt(sql);
        foreach (DataRow dr2 in dt2.Rows)
        {
            dr = dt.NewRow();
            dr["text"] = dr2["RealName"].ToString().Trim();
            dr["value"] = Convert.ToInt32(dr2["Guid"]);
            dt.Rows.Add(dr);
        }
        return dt;
    }

    public static DataTable Experts(bool hasAll)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr;
        if (hasAll)
        {
            dr = dt.NewRow();
            dr["text"] = "所有评审专家";
            dr["value"] = -1;
            dt.Rows.Add(dr);
        }
        dr = dt.NewRow();
        dr["text"] = "无";
        dr["value"] = 0;
        dt.Rows.Add(dr);
        string sql = "select u.Guid,u.RealName from Users u Join Doctor d on u.Guid=d.UserGuid where u.UserType='" + LwUser.DoctorType + "' and d.Expert=1 order by u.Guid desc";
        DataTable dt2 = Fun.CreateDt(sql);
        foreach (DataRow dr2 in dt2.Rows)
        {
            dr = dt.NewRow();
            dr["text"] = dr2["RealName"].ToString().Trim();
            dr["value"] = Convert.ToInt32(dr2["Guid"]);
            dt.Rows.Add(dr);
        }
        return dt;
    }

    public static bool DoctorHasStudent(int dg,int sg)
    {
        string sql = "select count(*) as n from Student where DoctorGuid="+dg.ToString()+" and UserGuid="+sg.ToString();
        int c = Convert.ToInt32(Fun.GetField(sql));
        return c > 0;
    }

    public static bool ExpertHasStudent(int eg, int sg)
    {
        string sql = "select count(*) as n from Student where ExpertGuid=" + eg.ToString() + " and UserGuid=" + sg.ToString();
        int c = Convert.ToInt32(Fun.GetField(sql));
        return c > 0;
    }

    public static void Delete(int g)
    {
        string sql = "select UserGuid from Student where DoctorGuid="+g.ToString();
        DataTable dt = Fun.CreateDt(sql);
        foreach (DataRow dr in dt.Rows)
        {
            Student.Delete(Convert.ToInt32(dr[0]));
        }
        sql = "select UserGuid from Student where ExpertGuid="+g.ToString();
        dt = Fun.CreateDt(sql);
        foreach (DataRow dr in dt.Rows)
        {
            Paper.Delete(Convert.ToInt32(dr[0]));
        }
        sql = "update Student set ExpertGuid=0 where ExpertGuid="+g.ToString();
        Fun.Execute(sql);
        sql = "delete from Doctor where UserGuid="+g.ToString();
        Fun.Execute(sql);
        LwUser.Delete(g);
    }
}